package one.stand.service.impl;

import com.github.pagehelper.PageHelper;
import one.stand.exception.ParamException;
import one.stand.model.ResultModel;
import one.stand.model.essay.TaskCustomerModel;
import one.stand.model.essay.TaskStaffModel;
import one.stand.request.essay.TaskCustomerForm;
import one.stand.request.essay.TaskCustomerQuery;
import one.stand.response.essay.TaskCustomerResponse;
import one.stand.service.BaseService;
import one.stand.service.ITaskCustomerSerivce;
import one.stand.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author luoyang
 * @date 2020/5/11 18:18
 */
@Service
public class TaskCustomerSerivceImpl extends BaseService implements ITaskCustomerSerivce {
    /**
     * 创建任务
     * @param request
     * @return
     */
    @Override
    public ResultModel<Boolean> addTaskCustomer(TaskCustomerForm request) throws ParamException {
        if(request.getStartTime().getTime() > request.getEndTime().getTime()){
            return ResultModel.fail("开始时间不能大于结束时间");
        }
        TaskCustomerModel model = new TaskCustomerModel();
        //按用户 公司 动态查询是否已经创建任务如果是则更新
        TaskCustomerModel oldModel = this.taskCustomerMapper.queryUserCompany(request);
        if(request.getTaskCustomerId() == null && oldModel == null){
            BeanUtils.copyProperties(request,model);
            //添加
            model.setCreatedTime(DateUtil.now());
            if(this.taskCustomerMapper.insert(model) > 0){
                batchStaff(request.getTaskUsers(),model);
                return ResultModel.success();
            }
        }else{
            if(oldModel == null){
                model = this.taskCustomerMapper.queryById(request.getTaskCustomerId().intValue());
            }else{
                model = oldModel;
            }

            if(model == null ){
               throw new ParamException("任务不存在");
            }
            BeanUtils.copyProperties(request,model,"id");
            if(taskCustomerMapper.update(model) > 0){
                //删除员工 在添加
                taskStaffMapper.deleteByTaskId(model.getId());
                batchStaff(request.getTaskUsers(),model);
                return ResultModel.success();
            }
        }
        return ResultModel.fail();
    }

    @Override
    public ResultModel<TaskCustomerResponse> queryTaskCustomer(TaskCustomerQuery request) {
        TaskCustomerResponse model = this.taskCustomerMapper.queryByIdRes(request.getTaskCustomerId().intValue());
        if(model == null){
            return ResultModel.noData();
        }
        TaskCustomerResponse response = new TaskCustomerResponse();
        BeanUtils.copyProperties(model,response);
        //查询员工
        response.setTaskUsers(this.taskStaffMapper.queryTaskUser(model.getTaskCustomerId()));
        return ResultModel.success(response);
    }

    /**
     * 批量添加
     * @param taskUsers
     * @param model
     */
    private void batchStaff(List<Integer> taskUsers,TaskCustomerModel model){
        List<TaskStaffModel> models = new ArrayList<>();
        taskUsers.forEach(user ->{
            TaskStaffModel taskStaffModel = new TaskStaffModel();
            taskStaffModel.setStartTime(model.getStartTime());
            taskStaffModel.setEndTime(model.getEndTime());
            taskStaffModel.setTaskId(model.getId());
            taskStaffModel.setCreatedTime(DateUtil.now());
            taskStaffModel.setState(0);
            taskStaffModel.setUserId(Long.valueOf(user));
            taskStaffModel.setViewState(0);
            taskStaffModel.setShareNum(0);
            taskStaffModel.setVisitNum(0);
            models.add(taskStaffModel);
        });
        this.taskStaffMapper.insert(models);
    }


}
